{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.20 \u901a\u8fc7\u5b57\u7b26\u4e32\u8c03\u7528\u5bf9\u8c61\u65b9\u6cd5\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u6709\u4e00\u4e2a\u5b57\u7b26\u4e32\u5f62\u5f0f\u7684\u65b9\u6cd5\u540d\u79f0\uff0c\u60f3\u901a\u8fc7\u5b83\u8c03\u7528\u67d0\u4e2a\u5bf9\u8c61\u7684\u5bf9\u5e94\u65b9\u6cd5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u7b80\u5355\u7684\u60c5\u51b5\uff0c\u53ef\u4ee5\u4f7f\u7528 getattr() \uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import math\n\nclass Point:\n    def __init__(self, x, y):\n        self.x = x\n        self.y = y\n\n    def __repr__(self):\n        return 'Point({!r:},{!r:})'.format(self.x, self.y)\n\n    def distance(self, x, y):\n        return math.hypot(self.x - x, self.y - y)\n\n\np = Point(2, 3)\nd = getattr(p, 'distance')(0, 0)  # Calls p.distance(0, 0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53e6\u5916\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528 operator.methodcaller() \uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import operator\noperator.methodcaller('distance', 0, 0)(p)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5f53\u4f60\u9700\u8981\u901a\u8fc7\u76f8\u540c\u7684\u53c2\u6570\u591a\u6b21\u8c03\u7528\u67d0\u4e2a\u65b9\u6cd5\u65f6\uff0c\u4f7f\u7528 operator.methodcaller \u5c31\u5f88\u65b9\u4fbf\u4e86\u3002\n\u6bd4\u5982\u4f60\u9700\u8981\u6392\u5e8f\u4e00\u7cfb\u5217\u7684\u70b9\uff0c\u5c31\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "points = [\n    Point(1, 2),\n    Point(3, 0),\n    Point(10, -3),\n    Point(-5, -7),\n    Point(-1, 8),\n    Point(3, 2)\n]\n# Sort by distance from origin (0, 0)\npoints.sort(key=operator.methodcaller('distance', 0, 0))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8c03\u7528\u4e00\u4e2a\u65b9\u6cd5\u5b9e\u9645\u4e0a\u662f\u4e24\u6b65\u72ec\u7acb\u64cd\u4f5c\uff0c\u7b2c\u4e00\u6b65\u662f\u67e5\u627e\u5c5e\u6027\uff0c\u7b2c\u4e8c\u6b65\u662f\u51fd\u6570\u8c03\u7528\u3002\n\u56e0\u6b64\uff0c\u4e3a\u4e86\u8c03\u7528\u67d0\u4e2a\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u9996\u5148\u901a\u8fc7 getattr() \u6765\u67e5\u627e\u5230\u8fd9\u4e2a\u5c5e\u6027\uff0c\u7136\u540e\u518d\u53bb\u4ee5\u51fd\u6570\u65b9\u5f0f\u8c03\u7528\u5b83\u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "operator.methodcaller() \u521b\u5efa\u4e00\u4e2a\u53ef\u8c03\u7528\u5bf9\u8c61\uff0c\u5e76\u540c\u65f6\u63d0\u4f9b\u6240\u6709\u5fc5\u8981\u53c2\u6570\uff0c\n\u7136\u540e\u8c03\u7528\u7684\u65f6\u5019\u53ea\u9700\u8981\u5c06\u5b9e\u4f8b\u5bf9\u8c61\u4f20\u9012\u7ed9\u5b83\u5373\u53ef\uff0c\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "p = Point(3, 4)\nd = operator.methodcaller('distance', 0, 0)\nd(p)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u8fc7\u65b9\u6cd5\u540d\u79f0\u5b57\u7b26\u4e32\u6765\u8c03\u7528\u65b9\u6cd5\u901a\u5e38\u51fa\u73b0\u5728\u9700\u8981\u6a21\u62df case \u8bed\u53e5\u6216\u5b9e\u73b0\u8bbf\u95ee\u8005\u6a21\u5f0f\u7684\u65f6\u5019\u3002\n\u53c2\u8003\u4e0b\u4e00\u5c0f\u8282\u83b7\u53d6\u66f4\u591a\u9ad8\u7ea7\u4f8b\u5b50\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}